package com.bc004346.travelus;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

import com.bc004346.travelus.Utility.Utility;

import java.text.ParseException;

import static com.bc004346.travelus.Utility.Utility.AVAILABILITY_REFRESH_ENABLED;
import static com.bc004346.travelus.Utility.Utility.AVAILABILITY_REFRESH_INTERVAL;
import static com.bc004346.travelus.Utility.Utility.DAYS_TO_SEARCH;
import static com.bc004346.travelus.Utility.Utility.PNR_REFRESH_ENABLED;
import static com.bc004346.travelus.Utility.Utility.PNR_REFRESH_INTERVAL;
import static com.bc004346.travelus.Utility.Utility.SEND_ERROR_REPORTS;
import static com.bc004346.travelus.Utility.Utility.log;

/**
 * A {@link PreferenceActivity} that presents a set of application settings. On
 * handset devices, settings are presented as a single list. On tablets,
 * settings are split by category, with category headers shown to the left of
 * the list of settings.
 * <p/>
 * See <a href="http://developer.android.com/design/patterns/settings.html">
 * Android Design: Settings</a> for design guidelines and the <a
 * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
 * API Guide</a> for more information on developing a Settings UI.
 */
public class SettingsActivity extends PreferenceActivity {

    /**
     * A preference value change listener that updates the preference's summary
     * to reflect its new value.
     */
    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            String stringValue = value.toString();
            preference.setSummary(stringValue);
            return true;
        }
    };

    /**
     * Binds a preference's summary to its value. More specifically, when the
     * preference's value is changed, its summary (line of text below the
     * preference title) is updated to reflect the value. The summary is also
     * immediately updated upon calling this method. The exact display format is
     * dependent on the type of preference.
     *
     * @see #sBindPreferenceSummaryToValueListener
     */
    private void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

        // Trigger the listener immediately with the preference's
        // current value.
        if (preference.getClass() == CheckBoxPreference.class) {
            sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                    PreferenceManager
                            .getDefaultSharedPreferences(this)
                            .getBoolean(preference.getKey(), false)
            );
        } else {
            sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                    PreferenceManager
                            .getDefaultSharedPreferences(this)
                            .getString(preference.getKey(), "")
            );
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        try {
            Utility.onStart(this);
        } catch (ParseException e) {
            log(e.getMessage());
        }
        setupSimplePreferencesScreen();
    }

    /**
     * Shows the simplified settings UI if the device configuration if the
     * device configuration dictates that a simplified, single-pane UI should be
     * shown.
     */
    private void setupSimplePreferencesScreen() {

        // Add 'general' preferences.
        addPreferencesFromResource(R.xml.pref_general);

        // Bind the summaries of EditText/List/Dialog/Ringtone preferences to
        // their values. When their values change, their summaries are updated
        // to reflect the new value, per the Android Design guidelines.
        bindPreferenceSummaryToValue(findPreference(PNR_REFRESH_ENABLED));
        bindPreferenceSummaryToValue(findPreference(PNR_REFRESH_INTERVAL));
        bindPreferenceSummaryToValue(findPreference(AVAILABILITY_REFRESH_ENABLED));
        bindPreferenceSummaryToValue(findPreference(AVAILABILITY_REFRESH_INTERVAL));
//        bindPreferenceSummaryToValue(findPreference(AUTO_CHECKIN_ENABLED));
        bindPreferenceSummaryToValue(findPreference(DAYS_TO_SEARCH));
        bindPreferenceSummaryToValue(findPreference(SEND_ERROR_REPORTS));
    }
}
